package com.xiguaji.tiktok.v1.tiktok.collect.repository;

import com.xiguaji.tiktok.utils.jdbc.JdbcTemplateKit;
import com.xiguaji.tiktok.utils.jdbc.PageResponse;
import com.xiguaji.tiktok.v1.tiktok.collect.dto.BloggerCollectResponse;
import com.xiguaji.tiktok.v1.tiktok.collect.dto.SearchData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class BloggerCollectionExtRepositoryImpl implements BloggerCollectionExtRepository {

    @Autowired
    JdbcTemplateKit jdbcTemplateKit;

    public PageResponse<BloggerCollectResponse> getPage(SearchData searchData, Long userId){
        StringBuffer sqlSelect = new StringBuffer("select c.id collectId,b.id bloggerId, b.o_uid uid," +
                "b.o_follower_count followerCount,b.o_avg_views avgViews,b.o_avg_likes avgLikes," +
                "b.o_avg_reviews avgReviews,b.o_avg_share avgShare,b.score,b.o_signature signature, " +
                "b.o_nickname nickname,b.o_unique_id uniqueId,b.o_avatar300 avatar,o_region region, " +
                "b.with_fusion_shop_entry withFusionShopEntry ,b.ins_id insId ,b.bio_email bioEmail , " +
                "b.youtube_channel_id youtubeChannelId ,b.twitter_id twitterId,b.bio_url bioUrl , " +
                "b.o_total_favorited likeCount, b.o_aweme_count awemeCount ");
        StringBuffer sql = new StringBuffer("from blogger_collection c " +
                "left join blogger_main b on c.blogger_id = b.o_uid " +
                "where c.user_id = ? ");
        List<Object> params = new ArrayList<>();
        params.add(userId);

        if(!StringUtils.isEmpty(searchData.getKeyword())){
            sql.append(" and (o_nickname like ? or b.o_unique_id like ?)");
            params.add("%"+searchData.getKeyword()+"%");
            params.add("%"+searchData.getKeyword()+"%");

        }
        String sort = "score";

        switch (searchData.getSort()){
            case 0:
                sort = "score";
                break;
            case 1:
                sort = "followerCount";
                break;
            case 2:
                sort = "likeCount";
                break;
            case 3:
                sort = "avgViews";
                break;
            case 4:
                sort = "avgLikes";
                break;
            case 5:
                sort = "avgReviews";
                break;
            case 6:
                sort = "avgShare";
                break;
            default:
                break;
        }

        sql.append("order by ").append(sort).append(" desc ");

        return jdbcTemplateKit.getPage(BloggerCollectResponse.class,searchData.getPage(),searchData.getPageSize(),sqlSelect.toString(),sql.toString(),params.toArray());
    }
}
